//
//  API.swift
//  GHMoyaNetWorkTest
//
//  Created by Guanghui Liao on 3/30/18.
//  Copyright © 2018 liaoworking. All rights reserved.
//https://github.com/Moya/Moya/blob/master/docs_CN/Examples/Basic.md

import Foundation
import Moya

enum API{
    case updateAPi(parameters:[String:Any])
    case register(email:String,password:String)
    case uploadHeadImage(parameters: [String:Any],imageDate:Data)//上传用户头像
    case baseRequset //选择域名
    case getSignKey(url:String) //获取signkey
    case getVPKey(url:String)  //获取vpkey
    case getGeo(url:String) //检测ip是否会被阻挡
    case login(url:String,playername:String,password:String,verifycode:String,sessionid:String) //登录
    case registerUser(url:String,parameters: [String:Any]) //注册
    case getBasicKey(url:String) //获取BasicKey
    case verifycode(url:String) //验证码接口
    
    case submitPhoneCallRequest(url:String,ccToken:String,vcToken:String,mobile:String,secondmobile:String,cid:String,scid:String,issue:String,callDate:String,callStart:String,callEnd:String) //电话回拨的提交
    
    case getQuestionList(url:String,ccToken:String,vcToken:String)//获取电话回拨的问题表格
    
    case memberInfoUL(url:String,ccToken:String,vcToken:String)//获取玩家首页资料
    //获取玩家红利以及反水信息
    case bonusinfo(url:String,ccToken:String,vcToken:String)
    case getDepositsPayList(url:String,ccToken:String,vcToken:String) //获取玩家可使用的三方充值模式列表
    case submitDeposits(url:String,amount:String,bankOption:String,bankDetails:String,ccToken:String,vcToken:String,isbq:String,payCardNumber:String,payUserName:String) //提交玩家存款
    case getATMList(url:String,ccToken:String,vcToken:String)  //获取ATM渠充值模式列表
    case getBankList(url:String,ccToken:String,vcToken:String) //获取网银充值模式列表
    case getATMHistory(url:String,bcid:String,ccToken:String,vcToken:String) //请求玩家ATM卡讯息
    case actionATMDeposit(url:String,bcid:String,ccToken:String,vcToken:String,transno:String?,deposituser:String,amount:String) //提交玩家ATM存款
    case updateDepositStatus(url:String,status:String,ccToken:String,vcToken:String) //更新玩家存款订单状态
    case requsetThirdParty(params:[String:Any]) //请求第三方存款
    case getWithdrawLimit(url:String)//玩家取款限制
    case getWithdrawBankList(url:String)//获取玩家取款卡列表
    case submitWithdrawFund(url:String,params:[String:Any])//提交提款基金
    case getVerificationState(url:String,pagetype:String,bankcode:String) //获取玩家验证状态
    case getVerificationAction(url:String,pagetype:String) //获取需验证详情
    case getPendingDepositStatus(url:String) //请求玩家有没有未审核银行充值
    case getGPSBalance(url:String,gpid:String,playername:String) //获取金额
    case fundreclaim(url:String,gpid:String) //回收金额到主账户
    case transaction(url:String,tout:String,tin:String,amount:String) //转出转入金额
    case transferAccounts(url:String,gpids:String) //回收金额到主账户
    case getAvailableGPS(url:String) //获取主账户金额
    case transferHistory(url:String,type:String,start:String,end:String,offset:String,pageCount:String) //转账历史 類別 (D 是存款 / T 是轉賬 / W 是取款)
    case isShowNewDeposits(url:String) //获取是否显示新的存款
    case getLastDepositsModel(url:String) //获取上以此访问的存款模式
    case updateNewDepositPage(url:String,newDepositPage:String) //更新玩家访问的存款模式
    case getDepositsList(url:String) //获取玩家可使用的充值模式列表
    case atmPendingTransaction(url:String)//检测是否有未完成提交的人工智能存款提案
    case getATMBankDetailInfo(url:String,amount:String)//人工智能存款获取银行卡详情
    case submitATMDeposit(url:String,bcid:String,amount:String,deposituser:String,transno:String)//提交人工智能存款
    case cancelATMDeposit(url:String,bcid:String,amount:String) //取消人工智能提款提案
    case paymentDepoist(url:String,bqoption:String,optionid:String,banklogo:String,amount:String,payCardNumber:String,payUserName:String)//提交存款（三方/bq/风云聚合）
    case xPayDeposit(url:String,expressCardNum:String,dno:String,expressCardAmount:String,expressCardName:String,ppid:String)
    case requsetEagleeyes(type:String,blackBox:String) //鹰眼
    case requsetEagleeyes2(url:String,blackBox:String,type:String,dno:String,ipaddress:String) //鹰眼2.0
    case bulkDeleteInternalMessage(url:String,ids:String,signature:String) //批量删除站内信信息
    case checkAccessRight(url:String) //查询玩家是否有电话回拨权限
    case goInGamePlatform(url:String,gpid:String)//进入游戏接口整合
    //    case memberInfoUL(url:String,cc:String,vc:String)
    case getActivityType(url:String)
    //取消玩家手机验证
    case cancelMobile(url:String,ccToken:String,vcToken:String)
    
    //获取域名接口配置
    case appchannel(tag: String , platform: Int)
}


extension API:TargetType{
    // 域名配置
    var baseURL: URL {
        switch self {
        case .requsetThirdParty:
            let tempUrl = URL.init(string: UserDefaults.standard.string(forKey: DefaultKey.THIRD_PARTY_DOMAIN) ?? "" )!
            return tempUrl
            
        case .requsetEagleeyes:
            return URL.init(string:"\(UserDefaults.standard.string(forKey: DefaultKey.DOMAIN_SCHEME)!)://\(UserDefaults.standard.string(forKey: DefaultKey.USER_DOMAIN)!)")!
            
        case .appchannel:
            return URL.init(string:"https://api.ydahu.com/api/v1/appchannel")!
            
        default:
            if(!isTest){
                // 正式域名
                let number = UserDefaults.standard.integer(forKey: DefaultKey.EAGLEEYES_NUMBER)
                if number == 2 {
                    return URL.init(string:"https://m.ahddz.com")!
                }else if number == 3{
                    return URL.init(string:"https://m.bloveapp.com")!
                }else{
                    return URL.init(string:"https://m.abawang.com")!
                }
            }else{
                return URL.init(string:GlobalInfo.urlTest)!
            }
            
        }
    }
    
    var path: String {
        switch self {
        case .baseRequset:
            return ""
        case .updateAPi:
            return "versionService.getAppUpdateApi"
        case .uploadHeadImage( _):
            return "/file/user/upload.jhtml"
        case .register( _, _):
            return ""
        case .getVPKey( _):
            return ""
        case .login(_, _, _,_,_):
            return ""
        case .registerUser( _, _):
            return ""
        case .getBasicKey( _):
            return ""
        case .submitPhoneCallRequest( _, _, _, _, _, _, _, _, _, _, _):
            return ""
        case .getQuestionList( _, _, _):
            return ""
        case .memberInfoUL( _, _, _):
            return ""
        case .bonusinfo( _, _, _):
            return ""
        case .getDepositsPayList( _, _, _):
            return ""
        case .submitDeposits( _, _, _, _, _, _,_, _,_):
            return ""
        case .getATMList( _, _, _):
            return ""
        case .getBankList( _, _, _):
            return ""
        case .getATMHistory( _, _, _, _):
            return ""
        case .actionATMDeposit( _, _,  _, _,  _,  _,  _):
            return ""
        case .updateDepositStatus( _, _,  _, _):
            return ""
        case .requsetThirdParty( _):
            return ""
        case .getWithdrawLimit( _):
            return ""
        case .getWithdrawBankList( _):
            return ""
        case .submitWithdrawFund( _, _):
            return ""
        case .getVerificationState( _, _,_):
            return ""
        case .getPendingDepositStatus( _):
            return ""
        case .getGPSBalance( _, _, _):
            return ""
        case .fundreclaim( _, _):
            return ""
        case .transaction(_, _,_, _):
            return ""
        case .getAvailableGPS(_):
            return ""
        case .transferHistory(_, _, _, _, _, _):
            return ""
        case .isShowNewDeposits(_):
            
            return ""
        case .requsetEagleeyes(_, _):
            return "/ClientsAPIs/eeTransaction"
            
        case .requsetEagleeyes2(_,_, _,_,_):
            return ""
        case .getLastDepositsModel(_):
            return ""
        case .getDepositsList(_):
            return ""
        case .updateNewDepositPage(_,_):
            return ""
        case .atmPendingTransaction(_):
            return ""
        case .getATMBankDetailInfo(_,_):
            return ""
        case .submitATMDeposit(_, _, _,_, _):
            return ""
        case .cancelATMDeposit(_, _, _):
            return ""
        case .paymentDepoist(_, _, _,_, _, _,_):
            return ""
        case .xPayDeposit(_, _, _,_, _, _):
            return ""
        case .verifycode( _):
            return ""
        case .getGeo( _):
            return ""
        case .getSignKey( _):
            return ""
        case .transferAccounts( _, _):
            return ""
        case .getVerificationAction(_, _):
            return ""
        case .bulkDeleteInternalMessage(_, _, _):
            return ""
        case .checkAccessRight(_):
            return ""
        case .goInGamePlatform(_, _):
            return ""
        case .getActivityType(_):
            return ""
        case .cancelMobile( _, _, _):
            return ""
        case .appchannel( _,_ ):
            return ""
            
        }
        
    }
    
    var method: Moya.Method {
        switch self {
        case .baseRequset:
            return .get
        default:
            return .post
        }
    }
    
    
    //    这个是做单元测试模拟的数据，必须要实现，只在单元测试文件中有作用
    var sampleData: Data {
        return "".data(using: String.Encoding.utf8)!
    }
    
    //    该条请API求的方式,把参数之类的传进来
    var task: Task {
        switch self {
        case let .register(email, password):
            return .requestParameters(parameters:API.setSignParams(params: ["email": email, "password": password]), encoding: JSONEncoding.default)
        case .baseRequset:
            return .requestPlain
        case let .updateAPi(parameters):
            return .requestParameters(parameters:API.setSignParams(params: parameters), encoding: URLEncoding.default)
        //图片上传
        case .uploadHeadImage(let parameters, let imageDate):
            ///name 和fileName 看后台怎么说，   mineType根据文件类型上百度查对应的mineType
            let formData = MultipartFormData(provider: .data(imageDate), name: "file",
                                             fileName: "hangge.png", mimeType: "image/png")
            return .uploadCompositeMultipart([formData], urlParameters: parameters)
        case .getVPKey(let url):
            return .requestParameters(parameters:API.setSignParams(params:["url": url]), encoding: JSONEncoding.default)
            
        case .login(let url, let playername, let password,let verifycode,let sessionid):
            var data =  ["playername":playername,"password":password]
            if verifycode != ""{
                data["verifycode"] = verifycode
            }
            if sessionid  != ""{
                data["jsessionid"] = sessionid
            }
            return .requestParameters(parameters: API.setSignParams(params:["url": url,"data":data]), encoding: JSONEncoding.default)
        case .getBasicKey(let url):
            return .requestParameters(parameters: API.setSignParams(params:["url": url]), encoding: JSONEncoding.default)
        case .submitPhoneCallRequest(let url, let ccToken, let vcToken, let mobile, let secondmobile, let cid, let scid, let issue, let callDate, let callStart, let callEnd):
            return .requestParameters(parameters: API.setSignParams(params:["url": url,"data":["cc":ccToken,"vc":vcToken,"mobile":mobile,"secondmobile":secondmobile,"cid":cid,"scid":scid,"issue":issue,"callDate":callDate,"callStart":callStart,"callEnd":callEnd]]), encoding: JSONEncoding.default)
        case .getQuestionList(let url, let ccToken, let vcToken):
            return .requestParameters(parameters: API.setSignParams(params:["url": url,"data":["cc":ccToken,"vc":vcToken]]), encoding: JSONEncoding.default)
        case .memberInfoUL(let url, let ccToken, let vcToken):
            return .requestParameters(parameters: API.setSignParams(params:["url": url,"data":["cc":ccToken,"vc":vcToken]]), encoding: JSONEncoding.default)
        case .bonusinfo(let url, let ccToken, let vcToken):
            return .requestParameters(parameters: API.setSignParams(params:["url": url,"data":["cc":ccToken,"vc":vcToken]]), encoding: JSONEncoding.default)
        case .getDepositsPayList(let url, let ccToken, let vcToken):
            return .requestParameters(parameters: API.setSignParams(params:["url": url,"data":["cc":ccToken,"vc":vcToken]]), encoding: JSONEncoding.default)
        case .submitDeposits(let url, let amount, let bankOption, let bankDetails, let ccToken, let vcToken,let isbq,let payCardNumber,let payUserName):
            var data = ["amount":amount,"bankOption":bankOption,"bankDetails":bankDetails,"cc":ccToken,"vc":vcToken,"isbq":isbq]
            if payCardNumber != "" && payUserName != ""{
                data["payCardNumber"] = payCardNumber
                data["payUserName"] =  payUserName
            }
            return .requestParameters(parameters: API.setSignParams(params:["url": url,"data":data]), encoding: JSONEncoding.default)
        case .getATMList(let url, let ccToken, let vcToken):
            return .requestParameters(parameters: API.setSignParams(params:["url": url,"data":["cc":ccToken,"vc":vcToken]]), encoding: JSONEncoding.default)
        case .getBankList(let url, let ccToken, let vcToken):
            return .requestParameters(parameters: API.setSignParams(params:["url": url,"data":["cc":ccToken,"vc":vcToken]]), encoding: JSONEncoding.default)
        case .getATMHistory(let url, let bcid, let ccToken, let vcToken):
            return .requestParameters(parameters: API.setSignParams(params:["url": url,"data":["cc":ccToken,"vc":vcToken,"bcid":bcid]]), encoding: JSONEncoding.default)
        case .actionATMDeposit(let url, let bcid, let ccToken, let vcToken, let transno, let deposituser, let amount):
            var data = ["cc":ccToken,"vc":vcToken,"bcid":bcid,"deposituser":deposituser,"amount":amount]
            if transno == nil {
                data["transno"] = ""
            }else{
                data["transno"] = transno
            }
            return .requestParameters(parameters: API.setSignParams(params:["url": url,"data":data]), encoding: JSONEncoding.default)
        case .updateDepositStatus(let url, let status, let ccToken, let vcToken):
            let data = ["cc":ccToken,"vc":vcToken,"status":status]
            return .requestParameters(parameters: API.setSignParams(params:["url": url,"data":data]), encoding: JSONEncoding.default)
        case .requsetThirdParty(let params):
            return .requestParameters(parameters: params, encoding: JSONEncoding.default)
        case .getWithdrawLimit(let url):
            return .requestParameters(parameters:API.setSignParams(params:["url": url,"data":API.setTokenParams(params: [:])]), encoding: JSONEncoding.default)
        case .getWithdrawBankList(let url):
            return .requestParameters(parameters:API.setSignParams(params:["url": url,"data":API.setTokenParams(params: [:])]), encoding: JSONEncoding.default)
        case .submitWithdrawFund(let url, let params):
            return .requestParameters(parameters:API.setSignParams(params:["url": url,"data": API.setTokenParams(params: params)]), encoding: JSONEncoding.default)
        case .getVerificationState(let url, let pagetype, let bankcode):
            var data = ["pagetype":pagetype,"token":"Qgx2kCCHz43lnQZmUDjejnm41RoNzpMEaOMbBrod"]
            if bankcode  != "" {
                data["bankcode"] = bankcode
            }
            return .requestParameters(parameters: API.setSignParams(params:["url": url,"data":API.setTokenParams(params: data)]), encoding: JSONEncoding.default)
        case .getPendingDepositStatus(let url):
            return .requestParameters(parameters:API.setSignParams(params:["url": url,"data":API.setTokenParams(params: [:])]), encoding: JSONEncoding.default)
        case .getGPSBalance(let url, let gpid, let playername):
            let data = ["currency":"CNY","gpid":gpid,"u":playername]
            return .requestParameters(parameters: API.setSignParams(params:["url": url,"data":API.setTokenParams(params: data)]), encoding: JSONEncoding.default)
        case .fundreclaim(let url, let gpid):
            let data = ["toid":gpid]
            return .requestParameters(parameters: API.setSignParams(params:["url": url,"data":API.setTokenParams(params: data)]), encoding: JSONEncoding.default)
        case .transaction(let url, let tout, let tin, let amount):
            let data = ["tout":tout,"tin":tin,"amount":amount]
            return .requestParameters(parameters: API.setSignParams(params:["url": url,"data":API.setTokenParams(params: data)]), encoding: JSONEncoding.default)
        case .getAvailableGPS(let url):
            return .requestParameters(parameters: API.setSignParams(params:["url": url,"data":API.setTokenParams(params: [:])]), encoding: JSONEncoding.default)
        case .transferHistory(let url, let type, let start, let end, let offset, let pageCount):
            let data = ["type":type,"start":start,"end":end,"offset":offset,"pageCount":pageCount]
            return .requestParameters(parameters: API.setSignParams(params:["url": url,"data":API.setTokenParams(params: data)]), encoding: JSONEncoding.default)
        case .isShowNewDeposits(let url):
            
            return .requestParameters(parameters:API.setSignParams(params:["url": url,"data":API.setTokenParams(params: [:])]), encoding: JSONEncoding.default)
        case .requsetEagleeyes(let type, let blackBox):
            let data = ["eeblackbox":blackBox,"type":type]
            return .requestParameters(parameters:API.setSignParams(params:["data":API.setTokenParams(params: data)]), encoding: JSONEncoding.default)
            
        case .requsetEagleeyes2(let url,let blackBox, let type, let dno,let ipaddress):
            var data = ["eeblackbox":blackBox,"type":type]
            if dno != ""{
                data["dno"] = dno
            }
            return .requestParameters(parameters: API.setSignParams(params:["url": url,"data":API.setTokenParams(params: data)]), encoding: JSONEncoding.default)
        case .getLastDepositsModel(let url):
            return .requestParameters(parameters: API.setSignParams(params:["url": url,"data":API.setTokenParams(params: ["isMobile":true])]), encoding: JSONEncoding.default)
        case .getDepositsList(let url):
            return .requestParameters(parameters: API.setSignParams(params:["url": url,"data":API.setTokenParams(params: [:])]), encoding: JSONEncoding.default)
        case .updateNewDepositPage(let url, let newDepositPage):
            return .requestParameters(parameters: API.setSignParams(params:["url": url,"data":API.setTokenParams(params: ["isMobile":true,"newDepositPage":newDepositPage])]), encoding: JSONEncoding.default)
        case .atmPendingTransaction(let url):
            return .requestParameters(parameters: API.setSignParams(params:["url": url,"data":API.setTokenParams(params: [:])]), encoding: JSONEncoding.default)
        case .getATMBankDetailInfo(let url, let amount):
            return .requestParameters(parameters:API.setSignParams(params: ["url": url,"data":API.setTokenParams(params: ["amount":amount])]), encoding: JSONEncoding.default)
        case .submitATMDeposit(let url, let bcid, let amount, let deposituser, let transno):
            return .requestParameters(parameters: API.setSignParams(params:["url": url,"data":API.setTokenParams(params: ["bcid":bcid,"amount":amount,"deposituser":deposituser,"transno":transno])]), encoding: JSONEncoding.default)
        case .cancelATMDeposit(let url, let bcid, let amount):
            return .requestParameters(parameters: API.setSignParams(params:["url": url,"data":API.setTokenParams(params: ["bcid":bcid,"amount":amount])]), encoding: JSONEncoding.default)
        case .paymentDepoist(let url, let bqoption, let optionid,let banklogo, let amount, let payCardNumber, let payUserName):
            
            var params = ["bqoption":bqoption ,"optionid":optionid,"amount":amount,"banklogo":banklogo]
            if payCardNumber != "" && payUserName != ""{
                params["payCardNumber"] = payCardNumber
                params["payUserName"] = payUserName
            }
            return .requestParameters(parameters: API.setSignParams(params:["url": url,"data":API.setTokenParams(params: params)]), encoding: JSONEncoding.default)
        case .xPayDeposit(let url, let expressCardNum, let dno, let expressCardAmount, let expressCardName, let ppid):
            
            let params = ["expressCardNum":expressCardNum ,"dno":dno,"expressCardAmount":expressCardAmount,"expressCardName":expressCardName,"ppid":ppid]
            
            return .requestParameters(parameters: API.setSignParams(params:["url": url,"data":API.setTokenParams(params: params)]), encoding: JSONEncoding.default)
            
        case .registerUser(let url, let parameters):
            return .requestParameters(parameters: API.setSignParams(params:["url": url,"data":parameters]), encoding: JSONEncoding.default)
        case .verifycode(let url):
            return .requestParameters(parameters: API.setSignParams(params:["url": url]), encoding: JSONEncoding.default)
        case .getGeo(let url):
            return .requestParameters(parameters: API.setSignParams(params:["url": url]), encoding: JSONEncoding.default)
        case .getSignKey(let url):
            return .requestParameters(parameters:["url": url], encoding: JSONEncoding.default)
        case .transferAccounts(let url, let gpids):
            return .requestParameters(parameters: API.setSignParams(params:["url": url,"data":API.setTokenParams(params: [:])]), encoding: JSONEncoding.default)
        case .getVerificationAction(let url, let pagetype):
            let data = ["pagetype":pagetype,"token":"Qgx2kCCHz43lnQZmUDjejnm41RoNzpMEaOMbBrod"]
            return .requestParameters(parameters: API.setSignParams(params:["url": url,"data":API.setTokenParams(params: data)]), encoding: JSONEncoding.default)
        case .bulkDeleteInternalMessage(let url, let ids, let signature):
            let data = ["id":ids,"signature":signature]
            return .requestParameters(parameters: API.setSignParams(params:["url": url,"data":API.setTokenParams(params: data)]), encoding: JSONEncoding.default)
        case .checkAccessRight(let url):
            return .requestParameters(parameters: API.setSignParams(params:["url": url,"data":API.setTokenParams(params: [:])]), encoding: JSONEncoding.default)
        case .goInGamePlatform(let url, let gpid):
            return .requestParameters(parameters: API.setSignParams(params:["url": url,"data":API.setTokenParams(params: ["gpid":gpid])]), encoding: JSONEncoding.default)
        case .getActivityType(let url):
            return .requestParameters(parameters: API.setSignParams(params: ["url":url,
                                                                             "data":API.setActivityParams()]), encoding: JSONEncoding.default)
        case .cancelMobile(let url, let ccToken, let vcToken):
            return .requestParameters(parameters: API.setSignParams(params:["url": url,"data":["cc":ccToken,"vc":vcToken]]), encoding: JSONEncoding.default)
            
        case .appchannel(let tag,let platform):
            return .requestParameters(parameters: API.setSignParams(params: ["tag":tag,"platform":platform]), encoding: JSONEncoding.default)
            
        }
    }
    
    static func setActivityParams() ->[String:Any]{
        let now = Date()
        let time = Int(now.timeIntervalSince1970)
        var tempParams = [String:Any]()
        tempParams["datetime"]=time
        tempParams["signature"] = stringToMD5(string: "datetime\(time)PfTezKrv4LdN45fCg8V2CH49cc56f")
        print("------------->\(tempParams)")
        return tempParams
    }
    
    static func setTokenParams(params:[String:Any])->[String:Any]{
        var tempParams = params
        tempParams["cc"] = API.getCcToken()
        tempParams["vc"] = API.getVcToken()
        return tempParams
    }
    
    static func setSignParams(params:[String:Any])->[String:Any]{
        var tempParams = params
        tempParams["sign"] = UserDefaults.standard.string(forKey: DefaultKey.SIGNKEY) ?? ""
        return tempParams
    }
    
    var headers: [String : String]? {
        return ["Content-Type":"application/json"]
        //  return ["Content-Type":"application/x-www-form-urlencoded"]
    }
    
    static func getCcToken() -> String{
        var cctoken = ""
        if(isTest){
            cctoken = UserDefaults.standard.string(forKey: DefaultKey.CCTOKEN81) ?? ""
        }else{
            cctoken = UserDefaults.standard.string(forKey: DefaultKey.CCTOKEN11) ?? ""
        }
        if cctoken == ""{
            return cctoken
        }
        return cctoken.removingPercentEncoding ?? ""
    }
    
    static func getVcToken() -> String{
        var vctoken = ""
        if(isTest){
            vctoken = UserDefaults.standard.string(forKey: DefaultKey.VCTOKEN81) ?? ""
        }else{
            vctoken = UserDefaults.standard.string(forKey: DefaultKey.VCTOKEN11) ?? ""
        }
        if vctoken == ""{
            return vctoken
        }
        return vctoken.removingPercentEncoding ?? ""
    }
    
}
